﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HPortal.Lib;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace HPortal.Module
{
    public partial class ArticleDetail : PanelControl
    {
        protected int ArticleID;
        protected int ChannelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ChannelID = DatabaseHelper.SelectScalarIntFromID("ChannelID", "Panel", PanelID);
                ArticleID = HtmlHelper.GetIdFromRequestString("ArticleID", 0);
                BindData();
            }
        }
        private void BindData()
        {
            DataTable art = DatabaseHelper.Select("[getArticleDetail] " + ArticleID);
            dtvArticleView.DataSource = art;
            dtvArticleView.DataKeyNames = new string[] { "ArticleID" };
            dtvArticleView.DataBind();
            field1 = (field1 == string.Empty) || field1 == null ? "5" : field1;
            string lastArticlePublishedTime = DateTime.Parse(art.Rows[art.Rows.Count - 1]["PublishedTime"].ToString()).ToString("MM/dd/yyyy HH:mm:ss");
            DataTable sa = DatabaseHelper.Select(new string[] { "Top " + field1 + " *" }, "Article", "ArticleID IN (Select ItemID From PanelList WHERE PanelID=" + PanelID + ") AND [Status]>2 AND PublishedTime<'" + lastArticlePublishedTime + "'", "PublishedTime DESC");
            dtlNew.DataSource = sa;
            dtlNew.DataBind();
            if (sa.Rows.Count.ToString() == field1)
            {
                lastArticlePublishedTime = DateTime.Parse(sa.Rows[sa.Rows.Count - 1]["PublishedTime"].ToString()).ToString("MM/dd/yyyy HH:mm:ss");
                object p = DatabaseHelper.SelectScalar("SELECT Top 1 PublishedTime FROM Article WHERE ArticleID IN (Select ItemID From PanelList WHERE PanelID=" + PanelID + ") AND [Status]>2 AND PublishedTime<'" + lastArticlePublishedTime + "' ORDER BY PublishedTime DESC");
                if (p != null)
                {
                    cmdContinue.Text = StringHelper.CaptionLanguage("Xem tiếp", "Continues");
                    cmdContinue.Visible = true;
                    cmdContinue.CommandArgument = DateTime.Parse(p.ToString()).ToString("MM/dd/yyyy HH:mm:ss");
                }
            }
        }
        public string WriteArticleImage(object ImageType)
        {

            if (ImageType != System.DBNull.Value)
            {
                string st = "<img src='/imgView.aspx?ArticleID=" + ArticleID + "' class='HeadImage' />";
                return st;
            }
            return string.Empty;
        }
        protected void cmdContinue_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}